package com.laikan.reader.bookstart.activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.bookreader.activity.BookDesActivity;
import com.laikan.reader.core.utils.BookFileUtils;
import com.laikan.reader.core.utils.PublicUtil;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 广告界面
 * Created by gwt on 2016/10/21.
 */
public class PosterActivity extends BasicActivity {

    private TextView tv_number;
    private Handler handler;
    private Timer timer;
    private RelativeLayout lk_poster_bg;
    private String posterAdverUrl;
    private String posterAdverTitle;

    @Override
    protected void assignViews() {
        setContentView(R.layout.activity_poster);
        tv_number = (TextView)findViewById(R.id.tv_number);
        lk_poster_bg = (RelativeLayout)findViewById(R.id.lk_poster_bg);
    }

    @Override
    protected void initData() {
        posterAdverUrl = SPUtil.getString("poster_advertise_url", "");
        posterAdverTitle = SPUtil.getString("poster_advertise_title", "广告");
        handler = new Handler();
        //        tv_number.setText("5");
        checkSplashBg();

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //                final String text = Integer.parseInt ((String)tv_number.getText()) - 1 + "";
                //                handler.post(new Runnable() {
                //                    @Override
                //                    public void run() {
                //                        tv_number.setText(text);
                //                    }
                //                });
                //                if ("1".equals(tv_number.getText())) {
                //
                //                    timeOver();
                //                    timer.cancel();
                //
                //                }

                timeOver();
                timer.cancel();

            }
        }, 3000);
    }

    @Override
    protected void setListener() {
        tv_number.setOnClickListener(this);
        lk_poster_bg.setOnClickListener(this);
    }

    @Override
    protected void onClickEvent(View view) {
        super.onClickEvent(view);
        switch (view.getId()) {
            case R.id.tv_number:
                MobclickAgent.onEvent(mContext, "gg-cp-tg"); //广告-闪频-点击
                timeOver();
                timer.cancel();

                break;
            case R.id.lk_poster_bg:
                MobclickAgent.onEvent(mContext, "gg-cp-dj");//广告-闪屏-跳过
                if (!TextUtils.isEmpty(posterAdverUrl)) {
                    if (posterAdverUrl.startsWith("http")) {
                        //H5链接
                        Intent intent = new Intent();
                        intent.setClass(mContext, PosterWeb.class);
                        intent.putExtra("url", posterAdverUrl);
                        intent.putExtra("title", posterAdverTitle);
                        startActivity(intent);
                        finish();
                    }else {
                        //是bookId
                        Intent intent = new Intent();
                        intent.setClass(mContext, BookDesActivity.class);
                        intent.putExtra("bookId", posterAdverUrl);
                        intent.setAction("from_poster");
                        startActivity(intent);
                        finish();
                    }
                }

                timer.cancel();
                break;
        }
    }

    private void timeOver() {

        if (SPUtil.getString("token", "").equals("")) {
            startActivity(new Intent(mContext, GuideActivity.class));
            finish();
        }else {
            Intent intent = new Intent(mContext, HomeActivity.class);
            intent.putExtra("login_type", "login_custom");
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        timer.cancel();
    }

    private void checkSplashBg() {
        String filePath = BookFileUtils.POSTERIMGFILEPATH + "/lk_poster_bg.png";
        File file = PublicUtil.getRootPath();
        file = new File(file.getPath() + filePath);
        lk_poster_bg.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeFile(file.getPath())));
    }
}
